Welcome to the PS2010 Code Bank. Here you will find a collection of the R code used for all data analyses covered across the module. Use the menu on the left hand side to navigate.
Some important tips:
The data used in all of this code will be referred to as “mydata”. Make sure you change this if you have called your data something else.
Where you see NULL, NULL1,
NULL2 etc is where you will need to add information based
on the data set you are using.
Change NULL to the name of the package.
Change NULL to the folder path.
Remember you can also set the working directory using the menu options:
Session -> Set Working Directory -> Choose Directory
mydata is the object that we save the data under. You
can call it whatever you like.
You can see the data in the Environment panel (top right in RStudio).
Change NULL to the name of the file.
Remember to add “.csv” at the end of the file name.
R only reads .csv data files.
Note: read_csv() requires the tidyverse package.
Change NULL to the name of the object/data file you want
to summarise.
Change NULL1 to the name of the object/data file you
want to create (i.e that will hold the descriptive statistics)
Change NULL2 to the name of the variable you want to
split the data by.
Change NULL3 to the name of the dependent variable you
want the descriptive statistics for.
See the example below:
Line by line analysis:
The first line creates an object called
summary_by_group which uses mydata AND
THEN
It will group mydata by the grouping variable, in
this example called drink AND THEN
It asks for the summarise() function.
It will call the mean mean and ask for the mean of
our dependent variable using mean(bpm)
It will call the standard deviation sd and ask for
the standard deviation of our dependent variable using
sd(bpm)
It will close the code with a final )